import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../model/cartmodellist.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
class SearchPage extends StatefulWidget {
  SearchPage({Key key}) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  List s3=['热搜'];
  searchs() async{
    SharedPreferences prefs=await SharedPreferences.getInstance();
 setState(() {
    s3=json.decode(prefs.getString('search'));
 });
 print('s3${s3}');
  }
  List a = [
    {"id": 0, "name": "义务"},
    {"id": 1, "name": "义务义务义务"},
    {"id": 2, "name": "义务义务"}
  ];
  List c = [
    {"id": 1, "name": "夏季新款少女感穿搭", "count": 2000000, "flag": true},
    {"id": 2, "name": "小个子女孩就穿短款连衣裙", "count": 1654578, "flag": true},
    {"id": 3, "name": "夏日诱惑蜜桃色", "count": 145587, "flag": false},
    {"id": 4, "name": "甜辣风女孩", "count": 131254, "flag": true},
    {"id": 5, "name": "爱上一个女孩", "count": 54512, "flag": false},
  ];
  //搜索历史区域
  List<Widget> _getsearch() => List.generate(s.length==0?s3.length:s.length, (index) {
        final ValueNotifier<List> _counter = ValueNotifier<List>([]);
       if(se==null){
         return  ValueListenableBuilder(valueListenable: _counter, builder: (BuildContext context,value,Widget child){
          InkWell(
             onTap:(){},
           child:Container(child: Text('热搜'), margin: EdgeInsets.fromLTRB(10, 0, 5, 0),
                    color: Colors.white10,)
          );
         });
       }else{
          return ValueListenableBuilder(
            valueListenable: _counter,
            child: null,
            builder: (BuildContext context, value, Widget child) {
              return InkWell(
                onTap: () {
                 // print(a[index]['id']);
                },
                child: Container(
                    margin: EdgeInsets.fromLTRB(10, 0, 5, 0),
                    color: Colors.white10,
                    child: Text('${se[index]}'),
                    )
                   // child: Text('${s==[]?s3[index]:s[index]}')),
              );
            });
       }
      });
  //热门榜单区域
  List<Widget> _getbanel() => List.generate(c.length, (index) {
        final ValueNotifier<List> _counter = ValueNotifier<List>([]);
        return ValueListenableBuilder(
            valueListenable: _counter,
            builder: (BuildContext context, value, Widget child) {
              return InkWell(
                  onTap: () {},
                  child: Container(
                    width: 350,
                    padding: EdgeInsets.all(5),
                    child: Row(
                      children: <Widget>[
                        Text('${c[index]['id']}',
                            style: TextStyle(
                                color: Colors.yellow[c[index]['id'] * 100],
                                fontSize: 18)),
                        SizedBox(
                          width: 5,
                        ),
                        Text('${c[index]['name']}'),
                        SizedBox(
                          width: 8,
                        ),
                        Text('${c[index]['count']}',
                            style:
                                TextStyle(fontSize: 12, color: Colors.black26)),
                        c[index]['flag']
                            ? IconButton(
                                icon: Icon(Icons.call_made),
                                iconSize: 16,
                                color: Colors.redAccent,
                                highlightColor: Colors.redAccent,
                                splashColor: Colors.redAccent,
                                disabledColor: Colors.redAccent,
                                onPressed: null)
                            : IconButton(
                                icon: Icon(Icons.call_received),
                                iconSize: 16,
                                color: Colors.redAccent,
                                highlightColor: Colors.greenAccent,
                                splashColor: Colors.greenAccent,
                                disabledColor: Colors.greenAccent,
                                onPressed: null)
                      ],
                    ),
                  ));
            });
      });
  var _username = new TextEditingController();
  FocusNode _focusNode = FocusNode();
  FocusScopeNode _focusScopeNode = FocusScopeNode();
  @override
  void initState() {
    this.getfocus();
   setState(() {
     // this.searchs();
   });
    getsearch();
    _focusNode.unfocus();
    _focusScopeNode.requestFocus(_focusNode);
    _focusNode.addListener(() {
      if (!_focusNode.hasFocus) {
        // _focusNode.requestFocus();
        _focusScopeNode.requestFocus(_focusNode);
        //_focusNode.requestFocus();
        //FocusScope.of(context).requestFocus(FocusNode());
        print('失去焦点');
        //  print(this._username.text);
      } else {
        _focusScopeNode.requestFocus(_focusNode);
        print('得到焦点');
      }
    });
  }

  getfocus() {
    print('我被触发了');
    _focusScopeNode.requestFocus(_focusNode);
  }
  
TextEditingController _textController= TextEditingController();
  Widget build(BuildContext context) {
    return Scaffold(
       resizeToAvoidBottomPadding: false, 
        appBar: AppBar(
          backgroundColor: Colors.orangeAccent,
          textTheme: TextTheme(),
          title:
           Container(
            //  height: ScreenUtil().setHeight(70),
            // padding: EdgeInsets.all(10),
              margin: EdgeInsets.only(right: 30),
              // height: 45,
             constraints: BoxConstraints(
                 // maxHeight: 55.0,
                 // minHeight: 30.0,
                ),
              child: SingleChildScrollView(
                // physics: BouncingScrollPhysics(),
                child: 
                TextField(
                style: TextStyle(color: Colors.black26),
                autocorrect: true,
                focusNode: _focusNode,
                 controller: _textController,
                  maxLines: null,
                  autofocus: true,
                  keyboardType: TextInputType.multiline,
                //  cursorColor: Colors.red,
                decoration: InputDecoration(
                    fillColor: Colors.white,
                    filled: true,
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20),
                    ),
                    suffixIcon: IconButton(
                        icon: Icon(Icons.search),
                        onPressed: () {
                         setState(() {
                            search( _textController.text);
                         });
                          print('我是历史${s}');
                         print(_textController.text);
                        }),
                    focusedBorder: OutlineInputBorder(
                      //选中时外边框颜色
                      borderRadius: BorderRadius.circular(20),
                      borderSide: BorderSide(
                        color: Colors.white,
                      ),
                    )),
              ),
              )
              ),
        ),
        body: InkWell(
            onTap: () {
              _focusNode.unfocus();
            },
            child: Container(
              child: Column(
                children: <Widget>[
                  //历史搜索标题
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        '历史搜索',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w600),
                      ),
                      IconButton(
                          icon: Icon(Icons.delete_forever),
                          onPressed: () {
                            setState(() {
                              remove();
                              s=[];
                              s3=[];
                             // a = [];
                              //print(a.clear())
                            });
                          })
                    ],
                  ),
                  //历史搜索主体区域
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Expanded(
                          child: Wrap(
                              spacing: 10,
                              runSpacing: 2,
                              children: _getsearch()))
                    ],
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  //全网热搜区域
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text('全网热搜',
                          style: TextStyle(
                              fontWeight: FontWeight.w700, fontSize: 18)),
                      // Text('更多热榜'),
                      IconButton(
                          icon: Icon(Icons.chevron_right), onPressed: null)
                    ],
                  ),
                  Column(
                    children: _getbanel(),
                  )
                ],
              ),
            )));
  }
}
